package app.beit.fintrack.controller.summarytrack;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import app.beit.fintrack.bean.summary.SummarySessionBean;
import app.beit.fintrack.bean.summary.SummaryZoneBean;
import app.beit.fintrack.controller.BaseController;
import app.beit.fintrack.controller.ResponseFintrack;
import app.beit.fintrack.controller.bean.SummaryTrackSessionBean;
import app.beit.fintrack.service.summarytrack.SummaryTrackService;
import app.beit.fintrack.utils.DateFormatUtils;
import app.beit.fintrack.utils.constr.MsgConstr;
import app.beit.fintrack.utils.constr.SessionConstr;

@Controller
@RequestMapping("/summary/chart")
public class SummaryTrackChartController extends BaseController {
	
	private final Logger log = Logger.getLogger(this.getClass());

//	@Autowired
//	private MasterService masterService;
	@Autowired
	private SummaryTrackService summaryTrackChartService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value="getsumprocess", method=RequestMethod.POST)
	@ResponseBody
	public ResponseFintrack<Map<String, List<SummaryZoneBean>>> getSumProcess(final HttpServletRequest request, final @RequestParam("fromDate") String fromDate,
			final @RequestParam("toDate") String toDate){
		this.log.info("get summary chart page");
		ResponseFintrack<Map<String, List<SummaryZoneBean>>> response = null;
		try{
			SummaryTrackSessionBean stsb = (SummaryTrackSessionBean) super.getAttributeInSession(request, SessionConstr.SESSION_SUMMARY);
			
			Date fd = DateFormatUtils.strToDate(fromDate);
			Date td = DateFormatUtils.strToDate(toDate);
	

			final Map<String, List<SummaryZoneBean>> maps = new HashMap<String, List<SummaryZoneBean>>();
			List<SummaryZoneBean> amountProcess = this.summaryTrackChartService.getSumTotalProcess(fd, td);
			List<SummaryZoneBean> amountPayable = this.summaryTrackChartService.getSumTotalPayable(fd, td);
			List<SummaryZoneBean> amountTransfer = this.summaryTrackChartService.getSumTotalTransfer(fd, td);
			
			maps.put("PROCESS", amountProcess == null ? new ArrayList<SummaryZoneBean>() : amountProcess);
			maps.put("PAYABLE", amountPayable == null ? new ArrayList<SummaryZoneBean>() : amountPayable);
			maps.put("TRANSFER", amountTransfer == null ? new ArrayList<SummaryZoneBean>() : amountTransfer);
			
			//set session search data
			stsb = stsb == null ? new SummaryTrackSessionBean() : stsb;
			stsb.setFromDate(fd);
			stsb.setFromDateStr(fromDate);
			stsb.setToDate(td);
			stsb.setToDateStr(toDate);
			
			request.getSession().setAttribute(SessionConstr.SESSION_SUMMARY, stsb);
			
			response = super.responseSuccess();
			response.setDatas(maps);
			this.log.info("receive datas : " + response.toString());
		}catch (Exception e){
			this.log.error(e.getMessage());
			e.printStackTrace();
			response = super.responseError(MsgConstr.ERROR_EXCEPTION_CODE, e.getMessage());
		}
		
		return response;
	}
	

	@SuppressWarnings("unchecked")
	@RequestMapping(value="getSummaryChartSession", method=RequestMethod.POST)
	@ResponseBody
	public ResponseFintrack<Map<String, List<SummaryZoneBean>>> getSummaryChartSession(final HttpServletRequest request){
		this.log.info("get summary chart from session");
		ResponseFintrack<Map<String, List<SummaryZoneBean>>> response = null;
		try{
			final SummarySessionBean ssb = (SummarySessionBean) request.getSession().getAttribute(SessionConstr.SESSION_SUMMARY);
			if(ssb != null){
				response = super.responseSuccess();
				response.setDatas(ssb.getMapTotalCharts());
			}
			
			ssb.setClickBack(false);
			request.getSession().setAttribute(SessionConstr.SESSION_SUMMARY, ssb);
			this.log.info("receive datas : " + response.toString());
		}catch (Exception e){
			this.log.error(e.getMessage());
			e.printStackTrace();
			response = super.responseError(MsgConstr.ERROR_EXCEPTION_CODE, e.getMessage());
		}
		
		return response;
	}
	
}
